package com.example.userdome.controller;

import com.example.userdome.pojo.ServiceResponse;
import com.example.userdome.pojo.User;
import com.example.userdome.service.IUserService;
import com.example.userdome.service.RegisterService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/api")
public class LoginSystemController {
    @Resource
    private IUserService iUserService;

    //登录方法----通过比较数据库与前端返回的密码与用户名数据，来判断是否登录成功
    @PostMapping("/login")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password) {
        User user = iUserService.login(username, password);
        if (user != null) {
            return "登录成功！";
        } else {
            return "登录失败！";
        }
    }
    @Resource
    private RegisterService registerService;

    //登录接口，当添加一条用户信息时，成绩表也增加一条
    @PostMapping("/register")
    public ResponseEntity<String> addStudentWithScore(@RequestBody User user) {
        //新增用户信息
        ServiceResponse result = registerService.addStudentWithScore(user);

        if (result.isResult()) {
            return ResponseEntity.ok("添加成功！！");
        }

        return ResponseEntity.badRequest().body(result.getMsg());
    }
    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:/api";
    }



}

